<?php
/**
 * Created by PhpStorm.
 * User: LYQ
 * Date: 2019/8/17
 * Time: 16:44
 */

namespace app\admin\controller;
use app\index\model\BaseModel;
use think\Controller;

class ActionImg extends Controller
{
    public function add(){
        //文件上传
        $allowedExts = array("gif","jpeg","jpg","png");
        $temp = explode(".",$_FILES["file"]["name"]);
        $extension = end($temp);//获取后缀名
        if ((($_FILES["file"]["type"] == "image/gif")
                || ($_FILES["file"]["type"] == "image/jpeg")
                || ($_FILES["file"]["type"] == "image/jpg")
                || ($_FILES["file"]["type"] == "image/pjpeg")
                || ($_FILES["file"]["type"] == "image/x-png")
                || ($_FILES["file"]["type"] == "image/png"))
            && ($_FILES["file"]["size"] < 204800)    // 小于 200 kb
            && in_array($extension, $allowedExts)){
            if($_FILES["file"]["error"]>0){
                echo "错误" .$_FILES["file"]["error"];
            }else{
                echo $_FILES["file"]["name"]."<br>";
                echo $_FILES["file"]["type"]."<br>";
                echo $_FILES["file"]["size"]."<br>";
                echo $_FILES["file"]["tmp_name"]."<br>";
                // 判断当期目录下的 upload 目录是否存在该文件
                // 如果没有 upload 目录，你需要创建它，upload 目录权限为 777
                if(file_exists("upload/".$_FILES["file"]["name"])){
                    echo "文件已存在";
                }else{
                    // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
                    move_uploaded_file($_FILES["file"]["tmp_name"], "../../school_img/lunbo_img/" . $_FILES["file"]["name"]);
                    $path = "../../school_img/lunbo_img/" . $_FILES["file"]["name"];
                    $id = Db::name('lunboImg')->insertGetId($path);

                }
            }
        }else{
            echo "非法的文件格式";
        }

    }
    public function del(){
        $value = I('id|name');
        Db::name($value['name'])->where('id','in',$value['id'])->delete();
    }
    public function edit(){
//文件上传
$allowedExts = array("gif","jpeg","jpg","png");
$temp = explode(".",$_FILES["file"]["name"]);
$extension = end($temp);//获取后缀名
if ((($_FILES["file"]["type"] == "image/gif")
        || ($_FILES["file"]["type"] == "image/jpeg")
        || ($_FILES["file"]["type"] == "image/jpg")
        || ($_FILES["file"]["type"] == "image/pjpeg")
        || ($_FILES["file"]["type"] == "image/x-png")
        || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 204800)    // 小于 200 kb
    && in_array($extension, $allowedExts)){
    if($_FILES["file"]["error"]>0){
        echo "错误" .$_FILES["file"]["error"];
    }else{
        echo $_FILES["file"]["name"]."<br>";
        echo $_FILES["file"]["type"]."<br>";
        echo $_FILES["file"]["size"]."<br>";
        echo $_FILES["file"]["tmp_name"]."<br>";
        // 判断当期目录下的 upload 目录是否存在该文件
        // 如果没有 upload 目录，你需要创建它，upload 目录权限为 777
        if(file_exists("upload/".$_FILES["file"]["name"])){
            echo "文件已存在";
        }else{
            // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
            move_uploaded_file($_FILES["file"]["tmp_name"], "../../school_img/lunbo_img/" . $_FILES["file"]["name"]);
            $path = "../../school_img/lunbo_img/" . $_FILES["file"]["name"];
            $id = $_POST['id'];
            Db::name('lunboImg')->where(['id'=>$id])->update(['lunbo_name'=>$path]);
        }
    }
}else{
    echo "非法的文件格式";
}

    }
}